home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / sound / dsond131.zip / PLAYSTER.C < prev   
C/C++ Source or Header  |  1994-03-06  |  6KB  |  215 lines

  1.  
  2. /*************************************************************************/
  3. /*                  PlayStereo.c                 */
  4. /*        Contains code used to play stereo samples         */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. extern UBYTE rightAMap[];
  23. extern UBYTE leftAMap[];
  24. extern UBYTE eitherAMap[];
  25. extern UBYTE bothAMap[];
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a stereo sample out of both speakers*/
  38. /*If the user specifies that just one of the two stereo channels will*/
  39. /*be played, DSound.c calls playMonoSample*/
  40.  
  41. void playStereoSample(BPTR leftFile,channel audioChannel,
  42.               struct Voice8Header *vhdr, ULONG length, char *filename)
  43. {
  44.    struct IOAudio *iob1_right,*iob2_right,*iob1_left,*iob2_left;
  45.    struct IOAudio *cur_right,*cur_left,*alt_right,*alt_left;
  46.    ULONG toRead;
  47.    ULONG amountLeft;
  48.    ULONG sampleSize=length;
  49.    BOOL done=FALSE;
  50.    BPTR rightFile;
  51.  
  52.    /*Open the file again*/
  53.    rightFile=dupFileHandle(leftFile,filename);
  54.  
  55.    /*And position ourselves at the start of the right channel's data*/
  56.    Seek(rightFile,length,OFFSET_CURRENT);
  57.  
  58.    /*Read the entire sample into memory, if specified*/
  59.    if(readAll)
  60.    {
  61.       storeLeft(leftFile,length,bufSize);
  62.       storeRight(rightFile,length,bufSize);
  63.       Close(rightFile);
  64.       leftFile=0L;
  65.       rightFile=4L;
  66.    }
  67.  
  68.    /*Get the first audio channel*/
  69.    iob1_left=GetAudioChannel(bufSize,leftAMap);
  70.    if(iob1_left==NULL)
  71.    {
  72.       WriteMsg("Couldn't create the first stereo buffer\n");
  73.       cleanup(150);
  74.    }
  75.  
  76.    iob1_right=GetAudioChannel(bufSize,rightAMap);
  77.    if(iob1_right==NULL)
  78.    {
  79.       WriteMsg("Couldn't create the second stereo buffer\n");
  80.       cleanup(150);
  81.    }
  82.  
  83.    /* If the user didn't specify a volume, get it from the VHDR */
  84.    if(volume==0)
  85.       volume=(vhdr->volume>>10);
  86.  
  87.    /* If the VHDR gave a volume of zero, use maximum volume*/
  88.    if(volume==0)
  89.       volume=64;
  90.  
  91.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  92.    /* rate found in the VHDR) */
  93.    if(speed==0)
  94.       speed=1000000000/(vhdr->samplesPerSec*279);
  95.    else
  96.       speed=1000000000/(speed*279);
  97.  
  98.    InitAudioChannel(iob1_left,volume,speed);
  99.    InitAudioChannel(iob1_right,volume,speed);
  100.  
  101.    /*Get the 2nd audio channel*/
  102.    iob2_left=DuplicateAudioChannel(iob1_left);
  103.  
  104.    if(iob2_left==NULL)
  105.    {
  106.       FreeAudioChannel(iob1_left);
  107.       FreeAudioChannel(iob1_right);
  108.       WriteMsg("Couldn't create the second buffer");
  109.       cleanup(175);
  110.    }
  111.  
  112.    iob2_right=DuplicateAudioChannel(iob1_right);
  113.    if(iob2_right==NULL)
  114.    {
  115.       FreeAudioChannel(iob1_left);
  116.       DeleteDuplication(iob2_left);
  117.       FreeAudioChannel(iob1_right);
  118.       WriteMsg("Couldn't create the second buffer");
  119.       cleanup(175);
  120.    }
  121.  
  122.    /* Load the first buffer*/
  123.    toRead=MIN(length,bufSize);
  124.    LoadAudioBuffer(leftFile,iob1_left,toRead);
  125.    LoadAudioBuffer(rightFile,iob1_right,toRead);
  126.    iob1_left->ioa_Length=iob1_right->ioa_Length=toRead;
  127.  
  128.    length-=toRead;
  129.    if(length==0 && loop)
  130.    {
  131.       length=sampleSize;
  132.       if(!readAll)
  133.       {
  134.      Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  135.      Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  136.       }
  137.    }
  138.  
  139.    /*Initialize the sample position info*/
  140.    updateSampleInfo(0,sampleSize,vhdr->samplesPerSec);
  141.  
  142.    /*And queue up the play requests*/
  143.    BeginIO((struct IORequest *)iob1_left);
  144.    BeginIO((struct IORequest *)iob1_right);
  145.  
  146.    cur_right=iob2_right;
  147.    cur_left=iob2_left;
  148.    alt_right=iob1_right;
  149.    alt_left=iob1_left;
  150.  
  151.    /*Loop while there's stuff to read*/
  152.    while(!done)
  153.    {
  154.       toRead=MIN(length,bufSize);
  155.  
  156.       if(toRead!=0)
  157.       {
  158.      LoadAudioBuffer(leftFile,cur_left,toRead);
  159.      LoadAudioBuffer(rightFile,cur_right,toRead);
  160.      cur_left->ioa_Length=cur_right->ioa_Length=toRead;
  161.      BeginIO((struct IORequest *)cur_left);
  162.      BeginIO((struct IORequest *)cur_right);
  163.      amountLeft=length-=toRead;
  164.  
  165.      if(length==0 && loop)
  166.      {
  167.         length=sampleSize;
  168.         if(!readAll)
  169.         {
  170.            Seek(leftFile,-sampleSize,OFFSET_CURRENT);
  171.            Seek(rightFile,-sampleSize,OFFSET_CURRENT);
  172.         }
  173.      }
  174.      done=FALSE;
  175.       }
  176.       else
  177.      done=TRUE;
  178.  
  179.       /*Wait for the buffer to finish*/
  180.       if((Wait(1<<cur_right->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit |
  181.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  182.      done=TRUE;
  183.  
  184.       /*Update the sample position info*/
  185.       updateSampleInfo(sampleSize-amountLeft-toRead,sampleSize,vhdr->samplesPerSec);
  186.  
  187.       /*If we got a message from the window, it is a CLOSEWINDOW message*/
  188.       /*and we're done*/
  189.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  190.       {
  191.      done=TRUE;
  192.       }
  193.  
  194.       swapPointers(&cur_left,&alt_left);
  195.       swapPointers(&cur_right,&alt_right);
  196.    }
  197.  
  198.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  199.    /*how much memory to free*/
  200.    iob1_left->ioa_Length=iob2_left->ioa_Length=bufSize;
  201.    iob1_right->ioa_Length=iob2_right->ioa_Length=bufSize;
  202.  
  203.    FreeAudioChannel(iob1_left);
  204.    DeleteDuplication(iob2_left);
  205.    FreeAudioChannel(iob1_right);
  206.    DeleteDuplication(iob2_right);
  207.  
  208.    if(rightFile != 4L)
  209.       Close(rightFile);
  210.  
  211.    return;
  212. }
  213.  
  214. /*End of PlayStereo.c*/
  215.